<?php

namespace App\Http\Controllers;

use App\Models\Ip;
use App\Transformers\IpUserTransformer;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class IpController extends Controller
{
    /**
     * @api {get} /api/ip?page=1&limit=2 ip列表
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "ip": "196.168.0.1"
     *               },
     *               {
     *                   "id": 2,
     *                   "ip": "196.168.0.2"
     *               }
     *           ],
     *           "meta": {
     *               "pagination": {
     *                   "total": 2,
     *                   "count": 2,
     *                   "per_page": 10,
     *                   "current_page": 1,
     *                   "total_pages": 1,
     *                   "links": null
     *               }
     *           }
     *       }
     */
    public function index(Request $request){
        $builder = Ip::query();

        $users = $builder->paginate($request->get('limit',10));

        return $this->response->paginator($users, new IpUserTransformer());
    }

    /**
     * @api {post} /api/ip ip添加
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} ip  ip（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function store(Request $request, Ip $ip){
        $validatedData = $request->validate([
            'ip' => 'required|ip',
        ]);
        Ip::create($validatedData);

        return $this->response->created();
    }

    /**
     * @api {patch} /api/ip/:ip ip修改
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} ip  ip（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     *       {
     *        "message": "修改成功"
     *       }
     */
    public function edit(Request $request, Ip $ip){
        $validatedData = $request->validate([
            'ip' => 'required|ip',
            'ip' => Rule::unique('ips')->ignore($ip->ip, 'ip')
        ]);

        $ip->update($validatedData);

        return response()->json(['message' => '修改成功']);
    }

    /**
     * @api {delete} /api/ip/:ip ip删除
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} ip  ip（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function destroy(Ip $ip){
        $ip->delete();
        return $this->response->noContent();
    }

    /**
     * @api {get} /api/ip/enabled ip白名单启用关闭
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} status  0关闭1启用
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function ipEnabled(Request $request){
        $validatedData = $request->validate([
            'status' => [
                'required',
                Rule::in(['0', '1']),
            ],
        ]);
        $data = [
            'IP_ENABLED'=> $validatedData['status']
        ];
        modifyEnv($data);
        return response()->json(['message' => '修改成功']);
    }

    /**
     * @api {get} /api/ip/enabled/index ip白名单状态
     * @apiGroup Ip
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *        {
     *           "data": {
     *           "enabled_status": "0"
     *       }
     */
    public function ipEnabledIndex(){
        $data = env('IP_ENABLED');
        $arr= [
            'enabled_status'=>$data
        ];
        return response()->json(['data' =>$arr]);
    }
}
